package com.cloudcc.frame.trigger;

import com.cloudcc.deveditor.core.CCObject;
import com.cloudcc.deveditor.trigger.TriggerEditor;
import com.cloudcc.frame.comclass.SaveAccountTest;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class Event_Test extends TriggerEditor {
    public void befoinsert() throws Exception {

        /*商机升迁，发送通知*/
        String eventType = "20211390340C7F7B9Sa4";
        String eleoppTYPE = "2022C1B60D1BFA64W8Fa";
        String ghztType = "已失效";
        String eleoppJieduanType = "1-发现机会";
        String eleoppJieduanType2 = "2-初次拜访";
        String frequece = "";
        String frequeceType = "低频";
        String recordType = record_new.get("recordtype") == null ? "" : record_new.get("recordtype") + "";
        String sctp1 = record_new.get("sctp") == null ? "" : record_new.get("sctp") + "";
//

        if (eventType.equals(recordType)) {
            String position = record_new.get("position") == null ? "" : record_new.get("position") + "";
            if (sctp1.equals("") && position.equals("")) {
                if (true) trigger.addErrorMessage("上传图片或者地址必填其一");
            }
            /*客户Id*/
            String relateid = record_new.get("relateid") == null ? "" : record_new.get("relateid") + "";
            String obj_suffix = "";
            if (!"".equals(relateid)) {
                obj_suffix = relateid.substring(0, 3);
            }
            String sql2 = "select SCHEMETABLE_NAME api from tp_sys_object where prefix='" + obj_suffix + "'";
            List<CCObject> list2 = this.cqlQuery("works", sql2);
            String api = "";
            String findAccount = "";
            if (list2.size() > 0) {
                api = list2.get(0).get("api") == null ? "" : list2.get(0).get("api") + "";
            }
            if (api.equals("account") || api.equals("Account")) {
                findAccount = "select jieduan,fk_psmc,id,name,ownerid,es_time,intention from eleopp where khmc ='" + relateid + "' and is_deleted ='0' ";
            } else if (api.equals("eleopp")) {
                String find = "select khmc from eleopp where id ='" + relateid + "' ";
                List<CCObject> eleopp = this.cqlQuery("eleopp", find);

                if (eleopp.size() > 0) {
                    relateid = eleopp.get(0).get("khmc") == null ? "" : eleopp.get(0).get("khmc") + "";
                }
                findAccount = "select jieduan,fk_psmc,id,name,ownerid,es_time,intention from eleopp where khmc ='" + relateid + "' and is_deleted ='0' ";
            }

            List<CCObject> accountInfoList = this.cqlQuery("eleopp", findAccount);
            if (accountInfoList.size() > 0) {
                String jieduan = accountInfoList.get(0).get("jieduan") == null ? "" : accountInfoList.get(0).get("jieduan") + "";
                String es_time = accountInfoList.get(0).get("es_time") == null ? "" : accountInfoList.get(0).get("es_time") + "";
                String intention = accountInfoList.get(0).get("intention") == null ? "" : accountInfoList.get(0).get("intention") + "";
                String fk_psmc = accountInfoList.get(0).get("fk_psmc") == null ? "" : accountInfoList.get(0).get("fk_psmc") + "";
                String eleoppName = accountInfoList.get(0).get("name") == null ? "" : accountInfoList.get(0).get("name") + "";
                String id = accountInfoList.get(0).get("id") == null ? "" : accountInfoList.get(0).get("id") + "";
                String ownerid = accountInfoList.get(0).get("ownerid") == null ? "" : accountInfoList.get(0).get("ownerid") + "";
                if (es_time.equals("")||intention.equals("")){
                    if (true)trigger.addErrorMessage("请填写建立联系时间，与意向");
                }
                String findeleNumSql = "select frequece from  ele_num where id ='" + fk_psmc + "' ";
                List<CCObject> ccObjects = this.cqlQuery("ele_num", findeleNumSql);
                if (ccObjects.size() > 0) {
                    frequece = ccObjects.get(0).get("frequece") == null ? "" : ccObjects.get(0).get("frequece") + "";
                }
                if ((jieduan.equals(eleoppJieduanType)) && (!frequece.equals(frequeceType))) {
                    String sctp = record_new.get("sctp") == null ? "" : record_new.get("sctp") + "";
                    if (!sctp.equals("")) {
                        String updateEleoppJIEduan = "update eleopp set jieduan ='" + eleoppJieduanType2 + "',recordtype='" + eleoppTYPE + "' where id ='" + id + "' ";
                        this.cqlQuery("eleopp", updateEleoppJIEduan);
                        CCObject noticeInfo = new CCObject("notice");
                        noticeInfo.put("companyName", eleoppName);
                        noticeInfo.put("accountId", ownerid);
                        noticeInfo.put("noticeInfo", "您的商机：" + eleoppName + ",已经升迁到第2阶段 ");
                        this.insert(noticeInfo);
                    } else {
                        final double EARTH_RADIUS = 6378137;
                        SaveAccountTest saveAccountTest = new SaveAccountTest(userInfo);
                        String gen = saveAccountTest.gen(position);
                        net.sf.json.JSONObject genInfo = net.sf.json.JSONObject.fromObject(gen);
                        net.sf.json.JSONArray geocodes = genInfo.getJSONArray("geocodes");
                        String location = geocodes.getJSONObject(0).getString("location");
                        String[] split = location.split(",");
                        /*纬度*/
                        String lon = split[0];
                        String yg_lon = "";
                        /*经度*/
                        String lat = split[1];
                        String yg_lat = "";
                        String findKeHu = "select id,name,dlwz from manage_group where fk_account = '" + relateid + "' and is_deleted='0' and ghzt !='" + ghztType + "' ";
                        List<CCObject> manageGroup = this.cqlQuery("manage_group", findKeHu);
                        if (manageGroup.size() > 0) {
                            String dlwz = manageGroup.get(0).get("dlwz") == null ? "" : manageGroup.get(0).get("dlwz") + "";
                            String[] split1 = dlwz.split(",");
                            yg_lon = split1[0];
                            yg_lat = split1[1];
                        }
                        if (yg_lon.equals("") || yg_lat.equals("")) {
                            if (true) trigger.addErrorMessage("当前管户小组的经纬度为空，请及时补充");
                        }
                        // 纬度
                        double lat1 = Math.toRadians(Double.parseDouble("".equals(lat) ? "0.00" : lat));
                        double lat2 = Math.toRadians(Double.parseDouble("".equals(yg_lat) ? "0.00" : yg_lat));
                        // 经度
                        double lng1 = Math.toRadians(Double.parseDouble("".equals(lon) ? "0.00" : lon));
                        double lng2 = Math.toRadians(Double.parseDouble("".equals(yg_lon) ? "0.00" : yg_lon));
                        // 纬度之差
                        double a = lat1 - lat2;
                        // 经度之差
                        double b = lng1 - lng2;
                        // 计算两点距离的公式
                        double distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
                                Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(b / 2), 2)));
                        // 弧长乘地球半径, 返回单位: 米
                        distance = distance * EARTH_RADIUS;
                        if (distance > 200) {
                            if (true) trigger.addErrorMessage("当前位置与客户位置之前距离大于200米");
                        } else {
                            String updateEleoppJIEduan = "update eleopp set jieduan ='" + eleoppJieduanType2 + "',recordtype='" + eleoppTYPE + "' where id ='" + id + "' ";
                            this.cqlQuery("eleopp", updateEleoppJIEduan);
                            CCObject noticeInfo = new CCObject("notice");
                            noticeInfo.put("companyName", eleoppName);
                            noticeInfo.put("accountId", ownerid);
                            noticeInfo.put("noticeInfo", "您的商机：" + eleoppName + ",已经升迁到第2阶段 ");
                            this.insert(noticeInfo);
                        }
                    }
                } else if ((jieduan.equals(eleoppJieduanType)) && (frequece.equals(frequeceType))) {
                    String updateEleoppJIEduan = "update eleopp set jieduan ='" + eleoppJieduanType2 + "',recordtype='" + eleoppTYPE + "' where id ='" + id + "' ";
                    this.cqlQuery("eleopp", updateEleoppJIEduan);
                    CCObject noticeInfo = new CCObject("notice");
                    noticeInfo.put("companyName", eleoppName);
                    noticeInfo.put("accountId", ownerid);
                    noticeInfo.put("noticeInfo", "您的商机：" + eleoppName + ",已经升迁到第2阶段 ");
                    this.insert(noticeInfo);
                }
            } else {
                if (true) trigger.addErrorMessage("没有查询到客户");
            }
        }
    }

    public static double teste() {
        String lon = "104.075931";
        String yg_lon = "106.513309";
        String lat = "30.651651";
        String yg_lat = "29.611235";
        final double EARTH_RADIUS = 6378137;
        // 纬度
        double lat1 = Math.toRadians(Double.parseDouble("".equals(lat) ? "0.00" : lat));
        double lat2 = Math.toRadians(Double.parseDouble("".equals(yg_lat) ? "0.00" : yg_lat));
        // 经度
        double lng1 = Math.toRadians(Double.parseDouble("".equals(lon) ? "0.00" : lon));
        double lng2 = Math.toRadians(Double.parseDouble("".equals(yg_lon) ? "0.00" : yg_lon));
        // 纬度之差
        double a = lat1 - lat2;
        // 经度之差
        double b = lng1 - lng2;
        // 计算两点距离的公式
        double distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
                Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(b / 2), 2)));
        // 弧长乘地球半径, 返回单位: 米
        distance = distance * EARTH_RADIUS;
        return distance;
    }

    public static void main(String[] args) {
        double teste = teste();
        System.out.println(teste);
    }
}
